package com.application.chiettinhdienluc.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.data.GlobalData;
import com.application.chiettinhdienluc.data.SharedPreferencesData;
import com.application.chiettinhdienluc.model.HinhAnhChiTietModel;
import com.application.chiettinhdienluc.model.VatTuChiTietModel;


public class HinhAnhChiTietDataSource {

	private MySQLiteHelper dbHelper;

	public static final String  HINHANHCHITIET_DB_NAME = "HSO_HINH_ANH";

	
	//"HSO_CHIETTINH_ID" VARCHAR, "File_Name" VARCHAR, "Ngay_Chup" DATETIME, "Submitted" BOOL
	
	//table column names
	private static final String HSO_CHIETTINH_ID = "HSO_CHIETTINH_ID";
	private static final String FILE_NAME = "File_Name";
	private static final String NGAY_CHUP = "Ngay_Chup";
	private static final String SUBMITTED = "Submitted";

	private final static String TAG = HinhAnhChiTietDataSource.class.getSimpleName();

	public HinhAnhChiTietDataSource() {
		dbHelper = MySQLiteHelper.getInstance();
	}	

	public long updateSumitted(String fileName) {
		long numRowEffect = 0;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();

				cv.put(SUBMITTED, 1 + "");
				
			

				String whereClause = "File_Name" + " = ? ";
				String[] whereArgs = new String[] { ""+fileName};

				numRowEffect = dbHelper.getDb().update(HINHANHCHITIET_DB_NAME, cv, whereClause, whereArgs);

				return numRowEffect;
			}	
		} catch (Exception e) {
		}
		finally{
		}

		return numRowEffect;
	}



	public ArrayList<HinhAnhChiTietModel> getHinhAnhChiTietByHoSoId(long hoSoId)
	{
		ArrayList<HinhAnhChiTietModel> hinhanhChiTiet = new ArrayList<HinhAnhChiTietModel>();
		Cursor cursor = null;
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				cursor = dbHelper.getDb().
						rawQuery("select HSO_CHIETTINH_ID, File_Name, Ngay_Chup, Submitted from " + HINHANHCHITIET_DB_NAME +
								" where HSO_CHIETTINH_ID = " + hoSoId, null); 
//								+
//								" and vt.MA_DVIQLY = '" + SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext()) + "'", null); 
				
				
				if (cursor != null && cursor.getCount() > 0) {
					
					int colId_HSO_CHIETTINH_ID = cursor.getColumnIndex(HSO_CHIETTINH_ID);
					int colId_FILE_NAME = cursor.getColumnIndex(FILE_NAME);
					int colId_NGAY_CHUP = cursor.getColumnIndex(NGAY_CHUP);
					int colId_SUBMITTED = cursor.getColumnIndex(SUBMITTED);
							
					cursor.moveToFirst();
					do {
						HinhAnhChiTietModel ha = new HinhAnhChiTietModel();
						
						ha.setHosochiettinhid(cursor.getString(colId_HSO_CHIETTINH_ID));
						ha.setFilename(cursor.getString(colId_FILE_NAME));
						ha.setNgaychup(cursor.getString(colId_NGAY_CHUP));
						ha.setSubmitted(cursor.getInt(colId_SUBMITTED));
						
						hinhanhChiTiet.add(ha);

					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}

		return hinhanhChiTiet;
	}
	
	public long addNewImageInfo(HinhAnhChiTietModel ha)
	{
		long row_id = -1; 
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();

				cv.put(HSO_CHIETTINH_ID, ha.getHosochiettinhid());
				cv.put(FILE_NAME, ha.getFilename());
				cv.put(NGAY_CHUP, ha.getNgaychup());
				cv.put(SUBMITTED, ha.getSubmitted());

				row_id = dbHelper.getDb().insert(HINHANHCHITIET_DB_NAME, null, cv);

				return row_id;
			}	
		} catch (Exception e) {
		}
		finally{
		}

		return row_id;
	}
	
	
	
	public int deleteImageByHoSoId(long hosoId, String fileName)
	{
		int numRowEffect = 0;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				String whereClause = " HSO_CHIETTINH_ID " + " = ? " + " and " + FILE_NAME + " = ? ";
				String[] whereArgs = new String[] {""+hosoId, fileName};

				numRowEffect = dbHelper.getDb().delete(HINHANHCHITIET_DB_NAME, whereClause, whereArgs);
			}
		} catch (Exception e) {
			numRowEffect = 0;
		}
		finally{
		}
		
		return numRowEffect;
		
	}
}